﻿namespace fastdbServer.BasicModel;

/// <summary>
///     返回结果的数据结构
/// </summary>
public class Result
{
    public int Code { get; set; }

    public string Msg { get; set; }

    public bool Success
    {
        get => Code == 0;
        set => Code = value ? 0 : -1;
    }

    public static Result Create(bool success = true, string msg = null, int code = 0)
    {
        return success ? CreateSuccess(msg) : CreateFailed(msg);
    }

    public static Result CreateSuccess(string msg = null, int code = 0)
    {
        return new Result { Code = code, Msg = "操作成功" };
    }

    public static Result CreateFailed(string msg = "操作失败", int code = -1)
    {
        return new Result { Code = code, Msg = msg };
    }
}

/// <summary>
///     带数据的返回格式
/// </summary>
/// <typeparam name="T"></typeparam>
public class DataResult<T> : Result
{
    public T Data { get; set; }

    public static DataResult<T> CreateData(T data, bool success = true, string msg = null, int code = 0)
    {
        return success ? CreateSuccessData(data, msg, code) : CreateFailedData(data, msg, code);
    }

    public static DataResult<T> CreateSuccessData(T data, string msg = null, int code = 0)
    {
        msg ??= "操作成功";
        return new DataResult<T> { Data = data, Code = code, Msg = msg };
    }

    public static DataResult<T> CreateFailedData(T data, string msg = null, int code = -1)
    {
        msg ??= "操作失败";
        return new DataResult<T> { Data = default, Code = code, Msg = msg };
    }
}